﻿#include "stdafx.h"
#include "S_MonitorDataContainer.h"

#include "Source/ProjectModule/Undo/S_UndoStackManager.h"
#include "Source/MainModule/Global/S_ConsoleManager.h"

#include "Source/Utils/Common/TTool.h"

/*
-----==========================================================-----
		类：		场强探头模块 - 数据容器.h
		作者：		drill_up
		所属模块：	场强探头模块
		功能：		【数据容器 + 增删改查 + 撤回】
					 （详细见cpp）
-----==========================================================-----
*/

S_MonitorDataContainer::S_MonitorDataContainer() {
	this->m_block_isAllReloading = false;
}
S_MonitorDataContainer::~S_MonitorDataContainer() {
}

/* --------------------------------------------------------------
----------SourceManager 单例
*/
S_MonitorDataContainer* S_MonitorDataContainer::project_manager = NULL;
S_MonitorDataContainer* S_MonitorDataContainer::getInstance() {
	if (project_manager == NULL) {
		project_manager = new S_MonitorDataContainer();
	}
	return project_manager;
}


/*-----------------------------------
		继承 - 某数据被添加
*/
void S_MonitorDataContainer::undoDataAdded(C_UndoDataBasePtr data, int index) {
	if (this->m_block_isAllReloading == false) {
		emit containerDataAdded(data, index);
	}
}
/*-----------------------------------
		继承 - 某数据被修改
*/
void S_MonitorDataContainer::undoDataModifyed(C_UndoDataBasePtr data, int index) {
	if (this->m_block_isAllReloading == false) {
		emit containerDataModifyed(data, index);
	}
}
/*-----------------------------------
		继承 - 某全局数据被修改
*/
void S_MonitorDataContainer::undoDataGlobalModifyed(C_UndoDataGlobalBasePtr data) {
	if (this->m_block_isAllReloading == false) {
		emit containerGlobalDataModifyed(data);
	}
}
/*-----------------------------------
		继承 - 某数据被删除
*/
void S_MonitorDataContainer::undoDataDeleted(C_UndoDataBasePtr data, int index) {
	if (this->m_block_isAllReloading == false) {
		emit containerDataDeleted(data, index);
	}
}
/*-----------------------------------
		消息 - 显示消息
*/
void S_MonitorDataContainer::u_showNewMessage(QString message) {
	S_ConsoleManager::getInstance()->append_InfoMsg(message);
}



/*-----------------------------------
		添加 - 场监视器（数组数据）
*/
void S_MonitorDataContainer::add(C_FieldMonitor data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::add(data.clonePtr());
}
/*-----------------------------------
		修改 - 场监视器（数组数据）
*/
void S_MonitorDataContainer::modify(C_FieldMonitor data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::modify(data.clonePtr());
}

/*-----------------------------------
		添加 - 场强探头（数组数据）
*/
void S_MonitorDataContainer::add(C_FieldProbe data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::add(data.clonePtr());
}
/*-----------------------------------
		修改 - 场强探头（数组数据）
*/
void S_MonitorDataContainer::modify(C_FieldProbe data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::modify(data.clonePtr());
}

/*-----------------------------------
		添加 - 电压监视器（数组数据）
*/
void S_MonitorDataContainer::add(C_VoltageMonitor data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::add(data.clonePtr());
}
/*-----------------------------------
		修改 - 电压监视器（数组数据）
*/
void S_MonitorDataContainer::modify(C_VoltageMonitor data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::modify(data.clonePtr());
}

/*-----------------------------------
		添加 - 电流监视器（数组数据）
*/
void S_MonitorDataContainer::add(C_CurrentMonitor data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::add(data.clonePtr());
}
/*-----------------------------------
		修改 - 电流监视器（数组数据）
*/
void S_MonitorDataContainer::modify(C_CurrentMonitor data) {
	Q_ASSERT(data.isNull() == false);
	S_UndoDataContainer::modify(data.clonePtr());
}



/*-----------------------------------
		修改（全局数据）

void S_MonitorDataContainer::modifyGlobal(C_PlaneWave data) {
	if (data.isNull()){ data.init(); }
	S_UndoDataContainer::modifyGlobal(data.clonePtr());
}*/

/*-----------------------------------
		获取（全局数据）

C_PlaneWave S_MonitorDataContainer::getDataPlaneWave(){
	C_UndoDataGlobalBasePtr data = this->getGlobalDataByClass("C_PlaneWave");
	if (data == nullptr){		//没有时，自动初始化
		data = QSharedPointer<C_PlaneWave>(new C_PlaneWave());
		data->init();
		this->m_undoDataGlobalTank.append(data);
	}
	QSharedPointer<C_PlaneWave> result = data.dynamicCast<C_PlaneWave>();
	return result->clone();
}*/


/*-----------------------------------
		删除 - 删除数据（根据标签）
*/
void S_MonitorDataContainer::deleteById(QString id) {
	C_UndoDataBasePtr data_ptr = this->getDataById(id);
	if (data_ptr == nullptr) { return; }
	Q_ASSERT(data_ptr.isNull() == false);
	S_UndoDataContainer::deleteById(data_ptr->getId());
}

/*-----------------------------------
		删除 - 删除数据（根据action）
*/
void S_MonitorDataContainer::deleteByAction() {
	QAction* cur_action = qobject_cast<QAction*>(this->sender());		//从action里面取出数据指针

	// 支持多选删除 2025-6-30 杨骁
	for (auto data_id : cur_action->data().toStringList())
	{
		this->deleteById(data_id);
	}
}

/*-----------------------------------
		复制 - 复制数据（根据ID）
*/
void S_MonitorDataContainer::copyById(QString id) {
	C_UndoDataBasePtr data_ptr = this->getDataById(id);
	Q_ASSERT(data_ptr.isNull() == false);
	S_UndoDataContainer::copy(data_ptr);
}
/*-----------------------------------
		复制 - 复制数据（根据action）
*/
void S_MonitorDataContainer::copyByAction() {
	QAction* cur_action = qobject_cast<QAction*>(this->sender());		//从action里面取出数据指针
	QString id = cur_action->data().value<QString>();
	this->copyById(id);
}


//获取 - 场强探头
S_UndoDataContainer_COMMON_DATA_GET_Method(S_MonitorDataContainer, C_FieldProbe, FieldProbe);
//获取 - 场监视器
S_UndoDataContainer_COMMON_DATA_GET_Method(S_MonitorDataContainer, C_FieldMonitor, FieldMonitor);
//获取 - 电压监视器
S_UndoDataContainer_COMMON_DATA_GET_Method(S_MonitorDataContainer, C_VoltageMonitor, VoltageMonitor);
//获取 - 电流监视器
S_UndoDataContainer_COMMON_DATA_GET_Method(S_MonitorDataContainer, C_CurrentMonitor, CurrentMonitor);


/*-------------------------------------------------
		获取 - 是否有表面电流设置
*/
bool S_MonitorDataContainer::hasCurrentSurfaceSetting() {
	QList<C_FieldMonitor> monitor_list = this->getDataFieldMonitorList();
	for (int i = 0; i < monitor_list.count(); i++) {
		C_FieldMonitor monitor = monitor_list.at(i);
		if (monitor.getMonitorType() == 1) {
			return true;
		}
	}
	return false;
}



/*-------------------------------------------------
		数据模型的隐藏 - 设置显示/隐藏（场强探头）
*/
void S_MonitorDataContainer::setVisible_Probe(QString data_id, bool visible) {
	if (visible == false) {		//（注意，hideList是指列表中有的id，反而不显示）
		if (this->m_dataHideList_Probe.contains(data_id)) { return; }
		this->m_dataHideList_Probe.append(data_id);
	}
	if (visible == true) {
		this->m_dataHideList_Probe.removeOne(data_id);
	}

	// > 发送信号
	emit signal_VisibleChanged_Probe(data_id, visible);
}
void S_MonitorDataContainer::setAllVisible_Probe(bool visible) {
	if (visible == false) {		//（注意，hideList是指列表中有的id，反而不显示）
		this->m_dataHideList_Probe = this->getDataFieldProbeAllId();
	}
	if (visible == true) {
		this->m_dataHideList_Probe.clear();
	}

	// > 发送信号
	QStringList id_list = this->getDataFieldProbeAllId();
	for (int i = 0; i < id_list.count(); i++) {
		emit signal_VisibleChanged_Probe(id_list.at(i), visible);
	}
}
/*-------------------------------------------------
		数据模型的隐藏 - 指定数据是否显示（场强探头）
*/
bool S_MonitorDataContainer::isVisible_Probe(QString data_id){
	return this->m_dataHideList_Probe.contains(data_id) == false;
}
/*-------------------------------------------------
		数据模型的隐藏 - 获取隐藏列表（场强探头）
*/
QStringList S_MonitorDataContainer::getHideList_Probe(){
	return this->m_dataHideList_Probe;
}


/*-------------------------------------------------
		数据模型的隐藏 - 设置显示/隐藏（电流监视器）
*/
void S_MonitorDataContainer::setVisible_CurrentMonitor(QString data_id, bool visible) {
	if (visible == false) {		//（注意，hideList是指列表中有的id，反而不显示）
		if (this->m_dataHideList_CurrentMonitor.contains(data_id)) { return; }
		this->m_dataHideList_CurrentMonitor.append(data_id);
	}
	if (visible == true) {
		this->m_dataHideList_CurrentMonitor.removeOne(data_id);
	}

	// > 发送信号
	emit signal_VisibleChanged_CurrentMonitor(data_id, visible);
}
void S_MonitorDataContainer::setAllVisible_CurrentMonitor(bool visible) {
	if (visible == false) {		//（注意，hideList是指列表中有的id，反而不显示）
		this->m_dataHideList_CurrentMonitor = this->getDataCurrentMonitorAllId();
	}
	if (visible == true) {
		this->m_dataHideList_CurrentMonitor.clear();
	}

	// > 发送信号
	QStringList id_list = this->getDataCurrentMonitorAllId();
	for (int i = 0; i < id_list.count(); i++) {
		emit signal_VisibleChanged_CurrentMonitor(id_list.at(i), visible);
	}
}
/*-------------------------------------------------
		数据模型的隐藏 - 指定数据是否显示（电流监视器）
*/
bool S_MonitorDataContainer::isVisible_CurrentMonitor(QString data_id){
	return this->m_dataHideList_CurrentMonitor.contains(data_id) == false;
}
/*-------------------------------------------------
		数据模型的隐藏 - 获取隐藏列表（电流监视器）
*/
QStringList S_MonitorDataContainer::getHideList_CurrentMonitor(){
	return this->m_dataHideList_CurrentMonitor;
}


/*-------------------------------------------------
		数据模型的隐藏 - 设置显示/隐藏（电压监视器）
*/
void S_MonitorDataContainer::setVisible_VoltageMonitor(QString data_id, bool visible) {
	if (visible == false) {		//（注意，hideList是指列表中有的id，反而不显示）
		if (this->m_dataHideList_VoltageMonitor.contains(data_id)) { return; }
		this->m_dataHideList_VoltageMonitor.append(data_id);
	}
	if (visible == true) {
		this->m_dataHideList_VoltageMonitor.removeOne(data_id);
	}

	// > 发送信号
	emit signal_VisibleChanged_VoltageMonitor(data_id, visible);
}
void S_MonitorDataContainer::setAllVisible_VoltageMonitor(bool visible) {
	if (visible == false) {		//（注意，hideList是指列表中有的id，反而不显示）
		this->m_dataHideList_VoltageMonitor = this->getDataVoltageMonitorAllId();
	}
	if (visible == true) {
		this->m_dataHideList_VoltageMonitor.clear();
	}

	// > 发送信号
	QStringList id_list = this->getDataVoltageMonitorAllId();
	for (int i = 0; i < id_list.count(); i++) {
		emit signal_VisibleChanged_VoltageMonitor(id_list.at(i), visible);
	}
}
/*-------------------------------------------------
		数据模型的隐藏 - 指定数据是否显示（电压监视器）
*/
bool S_MonitorDataContainer::isVisible_VoltageMonitor(QString data_id){
	return this->m_dataHideList_VoltageMonitor.contains(data_id) == false;
}
/*-------------------------------------------------
		数据模型的隐藏 - 获取隐藏列表（电压监视器）
*/
QStringList S_MonitorDataContainer::getHideList_VoltageMonitor(){
	return this->m_dataHideList_VoltageMonitor;
}



/*-----------------------------------
		数据 - 清除当前管理器数据
*/
void S_MonitorDataContainer::clearAllData_noSignal() {
	S_UndoDataContainer::clearTank();
}
/*-----------------------------------
		数据 - 清除当前管理器数据
*/
void S_MonitorDataContainer::clearAllData() {
	this->clearAllData_noSignal();
	emit containerDataAllReloaded();
}
/*-----------------------------------
		数据 - 获取存储的名称
*/
QString S_MonitorDataContainer::getSaveName() {
	return "S_MonitorDataContainer";
}
/*-----------------------------------
		数据 - 全部管理器数据 -> QJsonObject
*/
QJsonObject S_MonitorDataContainer::getAllDataOfJsonObject() {
	QJsonObject obj_all = QJsonObject();

	// > 数组数据 - 场强探头
	QJsonObject obj_FieldProbe = QJsonObject();
	obj_FieldProbe.insert("type", "FieldProbe");
	QJsonArray arr_FieldProbe = QJsonArray();
	QList<C_FieldProbe> list_FieldProbe = this->getDataFieldProbeList();
	for (int i = 0; i < list_FieldProbe.size(); i++){
		C_FieldProbe c = list_FieldProbe.at(i);
		arr_FieldProbe.push_back(c.getJsonObject());
	}
	obj_FieldProbe.insert("data_list", arr_FieldProbe);

	// > 数组数据 - 场强探头
	obj_all.insert("FieldProbe", obj_FieldProbe);
	

	// > 数组数据 - 场监视器
	QJsonObject obj_FieldMonitor = QJsonObject();
	obj_FieldMonitor.insert("type", "FieldMonitor");
	QJsonArray arr_FieldMonitor = QJsonArray();
	QList<C_FieldMonitor> list_FieldMonitor = this->getDataFieldMonitorList();
	for (int i = 0; i < list_FieldMonitor.size(); i++) {
		C_FieldMonitor c = list_FieldMonitor.at(i);
		arr_FieldMonitor.push_back(c.getJsonObject());
	}
	obj_FieldMonitor.insert("data_list", arr_FieldMonitor);

	// > 数组数据 - 场监视器
	obj_all.insert("FieldMonitor", obj_FieldMonitor);


	// > 数组数据 - 电压监视器
	QJsonObject obj_VoltageMonitor = QJsonObject();
	obj_VoltageMonitor.insert("type", "VoltageMonitor");
	QJsonArray arr_VoltageMonitor = QJsonArray();
	QList<C_VoltageMonitor> list_VoltageMonitor = this->getDataVoltageMonitorList();
	for (int i = 0; i < list_VoltageMonitor.size(); i++) {
		C_VoltageMonitor c = list_VoltageMonitor.at(i);
		arr_VoltageMonitor.push_back(c.getJsonObject());
	}
	obj_VoltageMonitor.insert("data_list", arr_VoltageMonitor);

	// > 数组数据 - 电压监视器
	obj_all.insert("VoltageMonitor", obj_VoltageMonitor);


	// > 数组数据 - 电流监视器
	QJsonObject obj_CurrentMonitor = QJsonObject();
	obj_CurrentMonitor.insert("type", "CurrentMonitor");
	QJsonArray arr_CurrentMonitor = QJsonArray();
	QList<C_CurrentMonitor> list_CurrentMonitor = this->getDataCurrentMonitorList();
	for (int i = 0; i < list_CurrentMonitor.size(); i++) {
		C_CurrentMonitor c = list_CurrentMonitor.at(i);
		arr_CurrentMonitor.push_back(c.getJsonObject());
	}
	obj_CurrentMonitor.insert("data_list", arr_CurrentMonitor);

	// > 数组数据 - 电流监视器
	obj_all.insert("CurrentMonitor", obj_CurrentMonitor);

	return obj_all;
}

/*-----------------------------------
		数据 - QJsonObject -> 全部管理器数据
*/
void S_MonitorDataContainer::setAllDataFromJsonObject(QJsonObject obj_all) {
	this->clearAllData_noSignal();
	this->m_block_isAllReloading = true;

	// > 数组数据 - 场强探头
	QJsonObject obj_FieldProbe = obj_all.value("FieldProbe").toObject();
	QJsonArray arr_FieldProbe = obj_FieldProbe.value("data_list").toArray();
	for (int i = 0; i < arr_FieldProbe.size(); i++){
		C_FieldProbe c = C_FieldProbe();
		c.setJsonObject(arr_FieldProbe.at(i).toObject());
		if (c.isNull()){ c.init(); }
		this->u_add(c.clonePtr());
	}

	// > 数组数据 - 场监视器
	QJsonObject obj_FieldMonitor = obj_all.value("FieldMonitor").toObject();
	QJsonArray arr_FieldMonitor = obj_FieldMonitor.value("data_list").toArray();
	for (int i = 0; i < arr_FieldMonitor.size(); i++) {
		C_FieldMonitor c = C_FieldMonitor();
		c.setJsonObject(arr_FieldMonitor.at(i).toObject());
		if (c.isNull()) { c.init(); }
		this->u_add(c.clonePtr());
	}

	// > 数组数据 - 电压监视器
	QJsonObject obj_VoltageMonitor = obj_all.value("VoltageMonitor").toObject();
	QJsonArray arr_VoltageMonitor = obj_VoltageMonitor.value("data_list").toArray();
	for (int i = 0; i < arr_VoltageMonitor.size(); i++) {
		C_VoltageMonitor c = C_VoltageMonitor();
		c.setJsonObject(arr_VoltageMonitor.at(i).toObject());
		if (c.isNull()) { c.init(); }
		this->u_add(c.clonePtr());
	}

	// > 数组数据 - 电流监视器
	QJsonObject obj_CurrentMonitor = obj_all.value("CurrentMonitor").toObject();
	QJsonArray arr_CurrentMonitor = obj_CurrentMonitor.value("data_list").toArray();
	for (int i = 0; i < arr_CurrentMonitor.size(); i++) {
		C_CurrentMonitor c = C_CurrentMonitor();
		c.setJsonObject(arr_CurrentMonitor.at(i).toObject());
		if (c.isNull()) { c.init(); }
		this->u_add(c.clonePtr());
	}

	this->m_block_isAllReloading = false;
	emit containerDataAllReloaded();
}